package com.buybuyyi.stylistcrm.order.dao.impl;

import com.buybuyyi.stylistcrm.order.dao.InvoiceOrderDao;
import com.buybuyyi.stylistcrm.order.helper.OrderGoodsHelper;
import com.buybuyyi.stylistcrm.order.mapper.InvoiceOrderMapper;
import com.buybuyyi.stylistcrm.order.po.InvoiceOrder;
import com.buybuyyi.stylistcrm.utils.DateUtil;
import com.buybuyyi.stylistcrm.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import stylistcrm.core.services.order.dto.InvoiceOrderDto;
import stylistcrm.core.services.order.dto.InvoiceOrderGoodsDto;
import stylistcrm.core.services.order.dto.QueryOrderConditionDto;
import stylistcrm.core.services.order.enums.EnumDeliveryStatus;
import stylistcrm.core.services.order.enums.EnumDeliveryWay;
import stylistcrm.core.services.order.enums.EnumOrderType;

import java.util.List;

/**
 * Created by cheny on 2016/9/4.
 */
@Repository
public class InvoiceOrderDaoImpl implements InvoiceOrderDao {

    @Autowired
    private InvoiceOrderMapper invoiceOrderMapper;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return invoiceOrderMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(InvoiceOrder record) {
        return invoiceOrderMapper.insert(record);
    }

    @Override
    public int insertSelective(InvoiceOrder record) {
        return invoiceOrderMapper.insertSelective(record);
    }

    @Override
    public InvoiceOrder selectByPrimaryKey(Integer id) {
        return invoiceOrderMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(InvoiceOrder record) {
        return invoiceOrderMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(InvoiceOrder record) {
        return invoiceOrderMapper.updateByPrimaryKey(record);
    }

    @Override
    public List<InvoiceOrderDto> getPageList(QueryOrderConditionDto queryCondition) {
        OrderGoodsHelper.setDefaultOrderBy(queryCondition, EnumOrderType.INVOICE_ORDER);
        List<InvoiceOrderDto> invoiceOrderDtoList=invoiceOrderMapper.getPageList(queryCondition);
        if(null!=invoiceOrderDtoList&&0!=invoiceOrderDtoList.size()){
            for(InvoiceOrderDto invoiceOrderDto:invoiceOrderDtoList){
                List<InvoiceOrderGoodsDto> goodsList=invoiceOrderDto.getGoods();
                if(null!=goodsList&&0!=goodsList.size()){
                    StringBuilder goodsNames=new StringBuilder();
                    int count=0;
                    for(int i=0;i<goodsList.size();i++){
                        InvoiceOrderGoodsDto orderGoodsDto=goodsList.get(i);
                        goodsNames.append(orderGoodsDto.getGoodsName());
                        if(i!=goodsList.size()-1){
                            goodsNames.append("、");
                        }
                        if(null!=orderGoodsDto.getCount()){
                            count+=orderGoodsDto.getCount();
                        }
                    }
                    invoiceOrderDto.setCount(count);
                    invoiceOrderDto.setGoodsNames(goodsNames.toString());
                }
                invoiceOrderDto.setDeliveryTime(DateUtil.formatDatetime(invoiceOrderDto.getDeliveryTime()));
                invoiceOrderDto.setCreateTime(DateUtil.formatDatetime(invoiceOrderDto.getCreateTime()));
                if(StringUtil.isNotBlank(invoiceOrderDto.getStatus())){
                    invoiceOrderDto.setStatus(EnumDeliveryStatus.getEnum(invoiceOrderDto.getStatus()).getDesc());
                }
                if(StringUtil.isNotBlank(invoiceOrderDto.getDeliveryWay())){
                    invoiceOrderDto.setDeliveryWay(EnumDeliveryWay.getEnum(invoiceOrderDto.getDeliveryWay()).getDesc());
                }
            }
        }
        return invoiceOrderDtoList;
    }

    @Override
    public List<Integer> getPageIdList(QueryOrderConditionDto queryCondition) {
        return invoiceOrderMapper.getPageIdList(queryCondition);
    }

    @Override
    public Integer getTotalCount(QueryOrderConditionDto queryCondition) {
        return invoiceOrderMapper.getTotalCount(queryCondition);
    }
}
